<template>
  <div class="login">
    
    <div class="select-btn-wrap">
      <div class="btn-wrap">
        <div
          class="btn-item"
          @click="change(0)"
          :class="{ activeBtn: isLogin }"
        >
          登录
        </div>
        <div
          class="btn-item"
          @click="change(1)"
          :class="{ activeBtn: !isLogin }"
        >
          注册
        </div>
      </div>
      <div class="form-wrap" v-if="isLogin">
        <div class="item">
          <div class="item-text">用户名</div>
          <input
            type="text"
            v-model="username"
            placeholder="请输入你的用户名"
          />
        </div>
        <div class="item">
          <div class="item-text">密码</div>
          <input
            type="password"
            v-model="password"
            placeholder="请输入你的密码"
          />
        </div>
        <div class="login-btn" @click="login">登录</div>
      </div>
      <div class="form-wrap" v-else>
        <div class="item">
          <div class="item-text">用户名</div>
          <input
            type="text"
            v-model="username"
            placeholder="请输入你的用户名"
          />
        </div>
        <div class="item">
          <div class="item-text">密码</div>
          <input
            type="password"
            v-model="password"
            placeholder="请输入你的密码"
          />
        </div>
        <div class="item">
          <div class="item-text">再次输入</div>
          <input
            type="password"
            v-model="repassword"
            placeholder="请重新输入你的密码"
          />
        </div>
        <div class="login-btn" @click="register">注册</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLogin: true,
      username: "",
      password: "",
      repassword: "",
    };
  },
  methods: {
    change(index) {
      this.username = "";
      this.password = "";
      if (index === 0) {
        this.isLogin = true;
      } else {
        this.isLogin = false;
      }
    },
    async login() {
      const res = await this.$http.post("http://localhost:3000/login", {
        username: this.username,
        password: this.password,
      });
        console.log(res);
      if (res.data.status === 200) {
        // alert("登录成功");
        console.log("登录成功");
        console.log(res);
        localStorage.setItem("user", JSON.stringify(res.data.data));
        this.$router.push("/about");
      } else {
        alert(res.data.statusText);
      }
    },
    async register() {
      if (this.password != this.repassword) {
        alert("两次输入的密码不一致");
        return;
      }
      const res = await this.$http.post("http://localhost:3000/register", {
        username: this.username,
        password: this.password,
      });
      if (res.data.status === 200) {
        // alert("注册成功");
        localStorage.setItem("user", JSON.stringify(res.data.data));
        this.$router.push("/about");
      } else {
        alert(res.data.statusText);
      }
    },
  },
};
</script>

<style lang='less' scoped>
.login{
  width: 100%;
  height: 810px;
  background: url(../assets/img/loginbg.png) ;
  padding:1px;
}
.select-btn-wrap {
  margin: 150px auto;
  margin-bottom: 0px;
  border: 1px solid black;
  width: 400px;
  //   height: 300px;
  border-radius: 15px;
  padding: 20px;
  .btn-wrap {
    //   border: 1px solid #ccc;
    display: flex;
    justify-content: space-around;
    font-size: 22px;
    font-weight: bold;
    color: #000;
    margin-bottom: 30px;
    cursor: pointer;
    user-select: none;
    .btn-item {
      padding: 0 20px;
      padding-bottom: 4px;
      border-bottom: 4px solid transparent;
    }
    .activeBtn {
      border-bottom: 4px solid #000;
    }
  }
  .form-wrap {
    padding: 0px 40px;
    .item {
      display: flex;
      justify-content: space-around;
      margin-bottom: 30px;
      .item-text {
        font-size: 16px;
        width: 120px;
      }
      input {
        padding: 10px 10px;
        outline: none;
        border-radius: 12px;
        border: 1px solid #ccc;
        //   font-size: 18px;
      }
      input:focus {
        border: 1px solid #38f;
      }
    }
    .login-btn {
      margin: 0px auto;
      width: 160px;
      padding: 10px 0;
      text-align: center;
      background-color: #38f;
      border-radius: 20px;
      cursor: pointer;
      color: #fff;
      user-select: none;
    }
    .login-btn:active {
      transform: scale(1.02);
    }
  }
}
</style>